package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lottery.ActivityLotteryConfig;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lottery.ActivityLotteryMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_LOTTERY_DRAW_NOTICE, desc = "抽奖公告")
public class GetActivityLotteryNoticeCmd extends AbstractCommand {

    int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.GetActivityLotteryNoticeReqMsg reqMsg = ActivityLotteryProto.GetActivityLotteryNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityLotteryConfig config = ActivityLotteryMgr.getActivityLotteryConfig(activityId);
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        ActivityLotteryProto.CrossGetActivityLotteryNoticeReqMsg.Builder builder = ActivityLotteryProto.CrossGetActivityLotteryNoticeReqMsg.newBuilder();
        builder.setActivityId(config.getActivityId());
        player.sendPacket(Protocol.C_ACTIVITY_LOTTERY_GET_NOTICE, builder);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ActivityLotteryProto.GetActivityLotteryNoticeRespMsg.Builder builder = ActivityLotteryProto.GetActivityLotteryNoticeRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setActivityId(activityId);
            player.sendPacket(Protocol.U_ACTIVITY_LOTTERY_DRAW_NOTICE, builder);
        }
    }
}
